package com.wechat.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wechat.admin.annoation.AuthCheck;
import com.wechat.admin.common.BaseResponse;
import com.wechat.admin.common.CommonConstants;
import com.wechat.admin.common.ResultUtils;
import com.wechat.admin.exception.ErrorCode;
import com.wechat.admin.exception.ThrowUtils;
import com.wechat.admin.model.enums.UserGroupEnum;
import com.wechat.admin.model.request.AddUserToGroupRequest;
import com.wechat.admin.model.request.GetUserAndDeviceInfoRequest;
import com.wechat.admin.model.request.UserGroupAddRequest;
import com.wechat.admin.model.vo.DeviceVo;
import com.wechat.admin.model.vo.UserAndDeviceVo;
import com.wechat.admin.model.vo.UserGroupVo;
import com.wechat.admin.service.UserDeviceLinkService;
import com.wechat.admin.service.WeUserGroupService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:sjb
 * @CreateTime:2024-12-15
 * @Description: 分组相关视图
 * @Version：1.0
 */
@RestController
@RequestMapping("/group")
public class GroupController {

    @Resource
    private WeUserGroupService userGroupService;

    @Resource
    private UserDeviceLinkService userDeviceLinkService;

    /**
     * 获取分组信息
     * @return
     */
    @GetMapping("/getUserGroup")
    public BaseResponse<List<UserGroupVo>> getUserGroup(){
        List<UserGroupVo> userGroupVoList = new ArrayList<>();
        for (UserGroupEnum value : UserGroupEnum.values()) {
            UserGroupVo userGroupVo = new UserGroupVo(value.getValue(), value.getText());
            userGroupVoList.add(userGroupVo);
        }
        return ResultUtils.success(userGroupVoList);
    }

    /**
     * 创建分组
     * @param userGroupAddRequest
     * @return
     */
    @PostMapping("/createGroup")
    @AuthCheck(mustRole = CommonConstants.ADMIN)
    public BaseResponse<Boolean> createGroup(@RequestBody UserGroupAddRequest userGroupAddRequest){
        Boolean userGroup = userGroupService.createUserGroup(userGroupAddRequest);
        return ResultUtils.success(userGroup);
    }

    /**
     * 添加组员信息到分组 只有管理员可操作
     * @param addUserToGroupRequest
     * @return
     */
    @PostMapping("/addUserToGroup")
    @AuthCheck(mustRole = CommonConstants.ADMIN)
    public BaseResponse<Boolean> addUserToGroup(@RequestBody AddUserToGroupRequest addUserToGroupRequest){
        ThrowUtils.throwIf(addUserToGroupRequest == null, ErrorCode.PARAMS_ERROR);
        Boolean added = userGroupService.addUserToGroup(addUserToGroupRequest);
        return ResultUtils.success(added);
    }


    /**
     * 管理员查看所有已分配登录的设备及用户信息
     * @param getUserAndDeviceInfoRequest
     * @return
     */
    //@AuthCheck(mustRole = Constants.ADMIN)
    @PostMapping("/admin/getDeviceListInfo")
    public BaseResponse<UserAndDeviceVo> getUserAndDeviceInfo(@RequestBody GetUserAndDeviceInfoRequest getUserAndDeviceInfoRequest){
        ThrowUtils.throwIf(getUserAndDeviceInfoRequest == null, ErrorCode.PARAMS_ERROR);

        UserAndDeviceVo userAndDevice = userGroupService.getUserAndDevice(getUserAndDeviceInfoRequest);
        return ResultUtils.success(userAndDevice);
    }

    /**
     * 用户本人查看设备信息
     * @param getUserAndDeviceInfoRequest
     * @return
     */
    @PostMapping("/user/deviceInfo")
    public BaseResponse<Page<DeviceVo>> getUserDeviceInfo(@RequestBody GetUserAndDeviceInfoRequest getUserAndDeviceInfoRequest){
        ThrowUtils.throwIf(getUserAndDeviceInfoRequest == null, ErrorCode.PARAMS_ERROR);
        Page<DeviceVo> userDeviceInfoByUserId = userDeviceLinkService.getUserDeviceLinkByUserId(getUserAndDeviceInfoRequest);
        return ResultUtils.success(userDeviceInfoByUserId);
    }
}
